﻿// See https://aka.ms/new-console-template for more information
using WeWorkSample;

Console.WriteLine("Hello, World!");

try
{
    Console.WriteLine("进入程序");
    var client = new FinanceSample("ww89b4f64a2eda9b00", "0RCZKYlgz6tYsNO18kF1DJq4THkB7EntpKw4nOLHFxw");
    var privateKey = @"<RSAKeyValue>
  <Modulus>jDdd56PEgFSnGNLqZqPbEskiJ62/TqptxRWNiixVn0AZliNLWhR/EAIkYKeHiW7MMR/sVuzpVmbE+6zjhHhk5zAeMAWifkTqurSMvQp8193c9kfNeMvI/7kHJmUClKMYee0VzDeAxt7wxV4biJDj1JPKDG0YjcCcz+jgFxkXEZ8=</Modulus>
  <Exponent>AQAB</Exponent>
  <P>txZya6wJRVtutUPunYe43CmnVXKbBtK1cNOnKMkjfNrjZnl5qPS4bxlS/rfTKjEyh+44gwBQm2wbSK373oylRw==</P>
  <Q>xA48OkkSyRE+9AkvVfnW+n/9Dbv2CCHSosYgCJKX8QsvXsrhnAc5QNW1Or4YpQkxFqwP0zm6KNmGrF3E1Og86Q==</Q>
  <DP>onTggnncoBZJXb4xdMaBWZn8SO2wcZV2Oe8UXDRdsOPo83zGXw0Guk4WPr6OONn+pG7U5ijZXY/Hhe4bl5Xk8Q==</DP>
  <DQ>QB+TzVB3J3JPj8utQsaLbLUN1D8k0jYVyY1ZjtQ7g+gvnyZmMf0e2bjMJKFmOBxQhOTT7DcWTyQOazPkG+q1eQ==</DQ>
  <InverseQ>Qc5NkC7wsySJKfFKhxyyyo1pSthfl1BDTpI7QoeI0ZedzTui204fkHP7/7uMyThyXvj+Y8T1CCA32WWPuiD1kw==</InverseQ>
  <D>NtBeMXPgGZr+R8IMSFu+BLYEWCagJwyRJP6r+lwijMLuPbdGecyRb5ymw/I6FgvuXzXvmnu7LD+4ufp4Zwz5xKLe9yiEC2TqGgdPs820JhBKi7UoOcej97H/87upejhFn/X89n6YVw6tS1fMNl+u8a2oIqweCG7FVf2AANZydME=</D>
</RSAKeyValue>";

    var (data, seq) = client.GetChatData(new Dictionary<int, string> { { 1, privateKey } }, 0, 10);

    Console.WriteLine("拉取会话完成：数据量：{0}", data.Count);
    var outputs = new List<string>();
    // 输出拉取的数据
    foreach (var item in data)
    {
        string fileName;
        byte[] bs;
        if (item.MsgType == "text")
        {
            outputs.Add($"text - time:{DateTimeOffset.FromUnixTimeMilliseconds(item.MsgTime).LocalDateTime}, content:{((ChatText)item).Text.Content}");
        }

        if (item.MsgType == "video")
        {
            fileName = $"{Guid.NewGuid()}.mp4";
            bs = client.GetMediaData(((ChatVideo)item).Video.SdkFileId);
            OutputFile(item, fileName, bs);
        }

        if (item.MsgType == "voice")
        {
            fileName = $"{Guid.NewGuid()}.amr";
            bs = client.GetMediaData(((ChatVoice)item).Voice.SdkFileId);
            OutputFile(item, fileName, bs);

        }

        if (item.MsgType == "image")
        {
            fileName = $"{Guid.NewGuid()}.jpg";
            bs = client.GetMediaData(((ChatImage)item).Image.SdkFileId);
            OutputFile(item, fileName, bs);
        }

        if (item.MsgType == "file")
        {
            var file = (ChatFile)item;
            fileName = $"{Guid.NewGuid()}.{file.File.FileExt}";
            bs = client.GetMediaData(file.File.SdkFileId);
            OutputFile(item, fileName, bs);

        }
    }

    Console.WriteLine("-----------------分割线------------------");

    foreach (var item in outputs)
    {
        Console.WriteLine(item);
    }

    void OutputFile(ChatBase item, string fileName, byte[] bs)
    {
        using (FileStream fs = new FileStream($@".\{fileName}", FileMode.Create))
        {
            fs.Write(bs, 0, bs.Length);
            fs.Flush();
            fs.Close();
        }
        outputs.Add($"{item.MsgType} - time:{DateTimeOffset.FromUnixTimeMilliseconds(item.MsgTime).LocalDateTime}, content:{fileName}");
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    Console.WriteLine("会话存档测试异常");
}